//
//  ReviewCurrentPositionViewController.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 14-6-23.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "ReviewCurrentPositionViewController.h"

#import "UserPosition.h"

NSString const* ReviewCurrentPositionViewControllerPositinAddressParamKey = @"ReviewCurrentPositionViewControllerPositinAddressParamKey";

@interface ReviewCurrentPositionViewController ()<BMKMapViewDelegate,XLGPositioningHelperDelegate>
{
    BMKMapView *_mapView;
    
    CLLocationCoordinate2D _coor;
    NSString *_address;
    
    XMSTimerBlock *_timer;
}
@end

@implementation ReviewCurrentPositionViewController


- (void)viewDidLoad
{
    [self setBackNavigateItemTitle:@"位置信息"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _mapView.delegate = self;
    _mapView.zoomLevel = 15;
    [self.view addSubview:_mapView];
    
//    [self enableNavBarLeftItemWhenProgressViewShow:YES];
    
    [XUINavigationController enableSwipeToLeftViewController:NO];
    
    NSString *address = self.param[ReviewCurrentPositionViewControllerPositinAddressParamKey];
    NSArray *ar = [address componentsSeparatedByString:@";"];
    
    _coor = CLLocationCoordinate2DMake(0, 0);
    _address = address;
    
    if(ar.count >= 3)
    {
        _coor = CLLocationCoordinate2DMake([ar[0] floatValue], [ar[1] floatValue]);
        _address = ar[2];
    }
    
    if(_address.length > 0 && _coor.latitude > 0 && _coor.longitude > 0)
    {
        _mapView.centerCoordinate = _coor;
        
        UserPosition *userL = [UserPosition new];
        [userL setCoordinate:_coor];
        if(_address.length >0)
            userL.title = _address;
        
        [_mapView addAnnotation:userL];
    }
    else
    {
        _timer = [[XMSTimerBlock alloc] initScheduledTimerWithTimeInterval:15 repeats:NO block:^(XMSTimerBlock *timer)
        {
            [_timer invalidate];
            
            [super endProgressViewPanel];
            
            [XLGPositioningHelper cancelReceiver:self];
            
            [UIAlertView showInfoMsg:@"地址无法显示"];
        }];
        
        if(_address.length > 0 && (_coor.latitude <= 0 || _coor.longitude <= 0))
        {
            [super beginProgressViewPanel:@"定位中，请稍候"];
            
            [XLGPositioningHelper getCoordWithReceiver:self addr:_address city:_address completion:NULL];
        }
        else if(_address.length <= 0 && _coor.latitude > 0 && _coor.longitude > 0)
        {
            [super beginProgressViewPanel:@"定位中，请稍候"];
            
            [XLGPositioningHelper getAddrWithReceiver:self coordinate:_coor completion:NULL];
        }
    }
}

-(void)viewControllerDidMoveDealloc
{
    [super viewControllerDidMoveDealloc];
    
    [super endProgressViewPanel];
    
    [_timer invalidate];
    _timer = nil;
    
    _mapView.delegate = nil;
    
    [XLGPositioningHelper cancelReceiver:self];
    
    [XUINavigationController enableSwipeToLeftViewController:YES];
}

#pragma mark --
#pragma mark -- mapView delegate

-(BMKAnnotationView*)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    BMKPinAnnotationView *annView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@""];
    [annView setSelected:YES];
    
    return annView;
}

-(void)positioningHelperUpdateAddr:(id<XLGPositingReverseGeoCodeResult>)addr userInfo:(id)userInfo error:(NSError *)error
{
    [_timer invalidate];
    
    [super endProgressViewPanel];
    
    [XLGPositioningHelper cancelReceiver:self];
    
    if(error == nil && addr.location.latitude > 0 && addr.location.longitude > 0)
    {
        _mapView.centerCoordinate = addr.location;
        
        UserPosition *userL = [UserPosition new];
        [userL setCoordinate:addr.location];
        
        userL.title = addr.address;
        
        [_mapView addAnnotation:userL];
    }
    else
    {
        [UIAlertView showInfoMsg:@"地址无法显示"];
    }
}

-(void)positioningHelperUpdateCoord:(CLLocation *)location userInfo:(id)userInfo error:(NSError *)error
{
    [_timer invalidate];
    
    [super endProgressViewPanel];
    
    [XLGPositioningHelper cancelReceiver:self];
    
    if(error == nil && location.coordinate.latitude > 0 && location.coordinate.longitude > 0)
    {
        _mapView.centerCoordinate = location.coordinate;
        
        UserPosition *userL = [UserPosition new];
        [userL setCoordinate:location.coordinate];
        if(_address.length >0)
            userL.title = _address.length > 0 ? _address : @"";
        
        [_mapView addAnnotation:userL];
    }
    else
    {
        [UIAlertView showInfoMsg:@"地址无法显示"];
    }
}

@end
